<template>
  <div>
    <van-nav-bar title="账号注册" left-arrow />

    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入账号"
        :rules="[
          { required: true, message: '请输入账号' },
          { required: true, message: '账号包含5-8个字符', pattern: /^\w{5,8}$/ }
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[
          { required: true, message: '请输入密码' },
          {
            required: true,
            message: '账号包含5-12个字符',
            pattern: /^\w{5,12}$/
          }
        ]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">注册</van-button>
      </div>
    </van-form>

    <p class="register" @click="fn">已有账号，去登录~</p>
  </div>
</template>

<script>
import {register} from "@/api/user"
export default {
  name: 'login',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      // console.log('submit', values)
      await register(values)
      this.$toast('注册成功')
      this.$router.push('/login')
    },
    fn(){
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="less" scoped>
.van-nav-bar .van-icon {
  color: #fff;
}
.van-nav-bar__title {
  color: #fff;
}
.register {
  text-align: center;
  margin-top: 20px;
  color: #696969;
  font-size: 14px;
}
</style>
